var urlLink = "http://"+window.location.host+"/";//获取当前域名
$(function () {
         $.ajax({
            type : "get",
            url : urlLink + "isLoged",
            data: {},
            dataType: 'json',
            success:function (Result) {
                var result = Result.result;
                if(result==1){
                    $(".pcc_gerenbnt").removeClass("hide");
                    $("#to-login").addClass("hide");
                    $("#to-reg").addClass("hide");
                }
            }
        });
        $("#pcc-logout").click(function () {
            $.ajax({
                type : "post",
                url : urlLink + "logout",
                data: {},
                dataType: 'json',
                success:function () {
                    location.href="login.html"
                }
            })
        })
        //分类信息查询
        getZhaorenFenleiList();
        //城市列表获取
        getCity();
        
        
        $('#login_do').click(function () {
            // var textarea=CKEDITOR.instances.pcc_cantent.getData();
            //   alert(textarea);
            var title=$('#pcc_title').val();
            var name1=$('#pcc_name1').val();
            var age=$('#pcc_age').val();
            var content= CKEDITOR.instances.pcc_cantent.getData();
            var username=$('#pcc_uresname').val();
            var A1=$('#A1').val();
            var A2=$('#A2').val();
            var A3=$('#A3').val();
            var classifyID=$('#classifyID').val();
            var phone=$('#reg_phone').val();
            var sex =$("input[name='sex']:checked").val();
            var istel =$("input[name='tel']:checked").val();
            var money =$('#pcc_money').val();
            var img = $('.pcc-xunrenimg').attr("src");
            var imgs=img.split("/")[2];
            
            if (title == "") {
                alert("标题不能为空");
                return false;
            }
            if (name1 == "") {
                alert("请填写您要寻找的亲人姓名");
                return false;
            }
            if (age == "") {
                alert("请填写您要寻找的亲人年龄");
                return false;
            }
            if(imgs==undefined){
                alert("请选择一张亲人图片上传吧");
                return false;
            }
            if (classifyID == 0) {
                alert("请选择亲人丢失原因");
                return false;
            }
            if (content == ""||content.length<80) {
                alert("请描述一下亲人走失信息，不少于80字。信息越多，找到可能行越大");
                return false;
            }
            if (username == "") {
                alert("请填写您的真实姓名");
                return false;
            }
            if (phone == "" || !/^[1]([3][0-9]{1}|[5][0-9]{1}|[8][0-9]{1})[0-9]{8}$/.test($('#reg_phone').val())) {
                alert("手机是我们联系您的唯一方式，请认真填写");
                return false;
            } else {
                 //提交个人中心
                $.ajax({
	            type: 'POST',
	            url : urlLink + "insertXunren",
	            data: {title:title,mobile:phone,contact:username,age:age,realName:name1,content:content,province:A1,city:A2,county:A3,money:money,isShow:istel,sex:sex,classifyID:classifyID,img:imgs},
	            dataType: 'json',
	            success:function (Result) {
	                   if(Result.result==1){
	                       alert("寻人信息添加成功，请等待审核");
	                       location.href="index.html";
	                   }else if(Result.result==2){
                           alert(Result.message);
                          location.href="home.html";
                        }else {
	                       alert(Result.message);
                       }
	                }
               });
            } 
        });
    });
    
    function getZhaorenFenleiList(){
           $.ajax({
            type: 'GET',
            url : urlLink + "getXunrenClassifyList",
            data: {},
            dataType: 'json',
            success:function (Result) {
                   var data =Result.data;
                   $(".xunrenfenlei").append("<option value='0'>请选择</option>");
                   $.each(data, function (index,item) {
                       $(".xunrenfenlei").append(
                          "<option value='"+item.id+"'>"+item.className+"</option>"
                       );
                })
            }
        }) 
    }
    function  getCity(){
          $("#A1").empty();
        //获取省列表
        $.ajax({
            type: 'GET',
            url : urlLink + "getindexDiquListByType",
            data: {types:1,pid:"0"},
            dataType: 'json',
            async:false,
            success:function (Result) {
                   var data =Result.data;
                   $.each(data, function (index,item) {
                       $('#A1').append("<option value='"+item.id+"'>"+item.diquName+"</option>");
                })
            }
         }) 
          getShi();
     }
     function getShi(){
           var sheng = $("#A1").val();
           $("#A2").empty();
           $.ajax({
            type: 'GET',
            url : urlLink + "getindexDiquListByType",
            data: {types:2,pid:sheng},
            dataType: 'json',
               async:false,
            success:function (Result) {
                   var data =Result.data;
                   $.each(data, function (index,item) {
                       $('#A2').append("<option value='"+item.id+"'>"+item.diquName+"</option>");
                })
            }
         }) 
         getQu();
     }
     function getQu(){
           var shi = $("#A2").val();
           $("#A3").empty();
           $.ajax({
            type: 'GET',
            url : urlLink + "getindexDiquListByType",
            data: {types:3,pid:shi},
            dataType: 'json',
               async:false,
            success:function (Result) {
                   var data =Result.data;
                   $.each(data, function (index,item) {
                       $('#A3').append("<option value='"+item.id+"'>"+item.diquName+"</option>");
                })
            }
         })
     }
